package com.ruoyi.train.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.mapper.CompanyMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.train.domain.Train;
import com.ruoyi.train.mapper.TrainMapper;
import com.ruoyi.train.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 公司培训记录Service业务层处理
 *
 * @author zjh
 * @date 2022-05-06
 */
@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainMapper trainMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 查询公司培训记录
     *
     * @param trainId 公司培训记录ID
     * @return 公司培训记录
     */
    @Override
    public Train selectTrainById(Long trainId) {
        return trainMapper.selectTrainById(trainId);
    }

    /**
     * 根据公司查询公司培训记录列表
     *
     * @return 公司培训记录
     */
    @Override
    public List<Train> selectTrainByCompanyId(Train train) {
        return trainMapper.selectTrainByCompanyId(train);
    }

    /**
     * 查询公司培训记录列表
     *
     * @param train 公司培训记录
     * @return 公司培训记录
     */
    @Override
    public List<Train> selectTrainList(Train train) {
        return trainMapper.selectTrainList(train);
    }

    /**
     * 新增公司培训记录
     *
     * @param train 公司培训记录
     * @return 结果
     */
    @Override
    public int insertTrain(Train train) {
        Long mainId = SecurityUtils.getLoginUser().getCompanyId();
        List<SysUser> users = userMapper.selectUserAll();
        List<Company> companies = companyMapper.selectAll();
        List<Train> trains = new ArrayList<>();
        String place = train.getTrainPlace();
        String mainpeople = train.getTrainMaster();
        Date dateall = train.getTrainDate();
        String contentall = train.getTrainContent();
        for (Company company : companies) {
            Train train1 = new Train();
            train1.setCompanyId(company.getCompanyId());
            if (train1.getCompanyId().equals(mainId)) {
                train1.setTrainMaster(mainpeople);
                train1.setTrainPlace(place);
            } else {
                train1.setTrainMaster(company.getCorporateRepresentative());
                train1.setTrainPlace("会议室");
            }
            train1.setTrainDate(dateall);
            train1.setTrainContent(contentall);
            String thisuser = "";
            for (SysUser user : users) {
                if (user.getDeptId().equals(user.getCompanyId())) {
                    continue;
                } else {
                    if (company.getCompanyId().equals(user.getCompanyId())) {
                        if (thisuser.equals("")) {
                            thisuser = user.getNickName();
                        } else {
                            thisuser = thisuser + "，" + user.getNickName();
                        }
                    }
                }
            }
            train1.setTrainPerson(thisuser);
            train1.setCreateTime(DateUtils.getNowDate());
            train1.setCreateBy(SecurityUtils.getLoginUser().getNickName());
            trains.add(train1);
        }
        return trainMapper.insertTimedTasks(trains);
    }

    /**
     * 修改公司培训记录
     *
     * @param train 公司培训记录
     * @return 结果
     */
    @Override
    public int updateTrain(Train train) {
        train.setUpdateTime(DateUtils.getNowDate());
        train.setUpdateBy(SecurityUtils.getLoginUser().getNickName());
        return trainMapper.updateTrain(train);
    }

    /**
     * 删除公司培训记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTrainByIds(String ids) {
        return trainMapper.deleteTrainByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公司培训记录信息
     *
     * @param trainId 公司培训记录ID
     * @return 结果
     */
    @Override
    public int deleteTrainById(Long trainId) {
        return trainMapper.deleteTrainById(trainId);
    }
}
